<?php
/**
 * 音乐控制器
 */
namespace Admin\Controller;
use Think\Controller;
class MusicController extends InitController {
    
	// 音乐列表
	public function musicList() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$musicModel = M('Mp3');
		$total = $musicModel->count();		
		
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		$list = $musicModel->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	
		$this->assign('list',$list);
		$this->assign('pager',$show);

		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);

		$this->display('Music/musicList');
	}
	
	//上传音乐
	public function upMusic(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Music/upMusic');
	}
	
	//上传音乐操作
	public function do_upMusic(){
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		$mp3 = I('mp3','','strip_tags,htmlspecialchars,trim');
		if(empty($title)){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入音乐名称！','field'=>''));
		}
		if(empty($mp3)){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请上传音乐文件！','field'=>''));
		}
		$data['title'] = $title;
		$data['url'] = $mp3;
		$data['createtime'] = NOW_TIME;
		$data['uid'] = session('uid'); //后台用户ID
		if (M('Mp3')->add($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	//上传音乐
	public function upMusicFile(){
		$info = $this->upload('media');
		$img = $info['savepath'] . $info['savename'];
		$this->ajaxReturn(array('error'=>0,'url'=>C('WEBSITE_URL').'/'.$img));
	}
	
	//删除音乐
	public function delMusic(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		if (M('Mp3')->where(array('id'=>$id))->delete()) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	//编辑音乐
	public function editMusic(){
		$id = I('id',0,'intval');
		if (!id) {
			$this->error('参数错误');
		}
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$musicModel = M('Mp3');
		$data =  $musicModel->where(array('id'=>$id))->find();
		
		$this->assign('data',$data);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Music/editMusic');
	}
	
	//编辑音乐操作
	public function do_editMusic(){
		$id = I('id',0,'intval');
		if(!$id){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		$mp3 = I('mp3','','strip_tags,htmlspecialchars,trim');
		if(empty($title)){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入音乐名称！','field'=>''));
		}
		if(empty($mp3)){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请上传音乐文件！','field'=>''));
		}
		$data['title'] = $title;
		$data['url'] = $mp3;
		
		if (M('Mp3')->where(array('id'=>$id))->save($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}

}